package BD;

import static com.sun.org.apache.xalan.internal.lib.ExsltDatetime.date;
import com.toedter.calendar.JDateChooser;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.SimpleTimeZone;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import sun.text.resources.FormatData;
import sun.util.calendar.CalendarDate;
public class utilsPim {
    String dataTexto ;
    
Date data = new Date(System.currentTimeMillis());    
Timestamp DataHoje = new Timestamp(System.currentTimeMillis());
//SimpleDateFormat formatarDate = new SimpleDateFormat("yyyy-MM-dd");   
//System.out.print(formatarDate.format(data));      
    public void MaxCaracter(JTextField f,int t){    
        f.setText( MaxCaracter(f.getText(), t)  );        
    }

    public String MaxCaracter(String s,int t){    
        String num = s;        
        int quantCaracteres = num.length();
        if (quantCaracteres > (t-1)){
        num = num.substring (0, num.length() - 1);
        }
        return (num); 
        
    }    
    public String getData(String formato) {
        SimpleDateFormat formatarDate = new SimpleDateFormat(formato);   
        return  formatarDate.format(data);
    }
    public String getDataHoje() {        
        data = new Date(System.currentTimeMillis());    
        SimpleDateFormat formatarDate = new SimpleDateFormat("dd/MM/yyyy");   
        return  formatarDate.format(data);
    }    
    
    public Timestamp getdataHoje() {
        DataHoje = new Timestamp(System.currentTimeMillis());
        return DataHoje ;
    }    
    
    public String getDataHoraHoje(){
        data = new Date(System.currentTimeMillis());    
        SimpleDateFormat formatarDate = new SimpleDateFormat("dd/MM/yyyy HH:mm");   
        return  formatarDate.format(data);
    }
    public String getHoraHoje() {
        SimpleDateFormat formatarDate = new SimpleDateFormat("HH:mm");   
        return  formatarDate.format(data);
    }
    public String getHoraStr(Time pTime,String pFormat){        
//       DateFormat f2 = DateFormat.getDateInstance(DateFormat.FULL,Locale.ROOT)        ;       
       //DateFormat f2 = DateFormat.getTimeInstance(Time.FULL,Locale.ROOT)        ;       
       SimpleDateFormat formatBra;     
       formatBra = new SimpleDateFormat("HH:mm");  

       try {  
              java.util.Date newData = formatBra.parse(pTime.toString());  
              return (formatBra.format(newData));  
       } catch (ParseException Ex) {  
            JOptionPane.showMessageDialog(null,"Erro na conversão da Hora(getHoraStr)"+ Ex.getMessage());
              return "";  
       }        
    }
    
    
    public String getDataStr(Date pData,String pFormat){        
       Date _pData = converteDataUtilToSql(pData);
       SimpleDateFormat formatBra;     
       formatBra = new SimpleDateFormat("yyyy-MM-dd");  
       try {  
              java.util.Date newData = formatBra.parse(pData.toString());  
              return (formatBra.format(newData));  
       } catch (ParseException Ex) {  
            JOptionPane.showMessageDialog(null,"Erro na conversão da data"+ Ex.getMessage());
              return "";  
       }        
    }
    public Date getDataDt(String data) throws Exception {   
        Date date = null;  
        
        if (data == null || data.equals(""))  
            return null;  
        date = getDataDt(data,"dd/MM/yyyy");
        
        return date;  
    }    
    
    public Date getDataDt(String data,String format) throws Exception {   
        if (data == null || data.equals(""))  
            return null;  

        Date date = null;  
        try {  
            DateFormat formatter = new SimpleDateFormat(format);  
            date = (java.util.Date)formatter.parse(data);  
        } catch (ParseException e) {              
            throw e;  
        }  
        return date;  
    }    
    public String getDataStr(Date pData){                
      return getDataStr(pData,"dd/MM/yyyy");
    }           

    public void setData(String datas) {
        this.dataTexto = datas;
    }
    
    public Integer getIndexCombobox(JComboBox obj, String pValue){
       int h = -1; 
       for(int i = 0; i < obj.getItemCount(); i++) {
           if(obj.getItemAt(i).equals(pValue)){
               h = i;
               break;
           }               
       }    
       return h;        
    }
    public void getIndexComboboxSet(JComboBox obj, String pValue){
        int h = getIndexCombobox(obj, pValue); 
        
        obj.setSelectedIndex(h);                        
        
    }
    public String getDateChooser(JDateChooser obj){
        int Dia = obj.getCalendar().get(Calendar.DAY_OF_MONTH);
        int Mes = obj.getCalendar().get(Calendar.MONTH);
        int Ano = obj.getCalendar().get(Calendar.YEAR);
        Mes = Mes +1;/*Adaptaçlão, a rotina só traz ate agora 27/10/14 p mes - 1 */
        return Dia+"/"+Mes +"/"+ Ano;
        
    }
    public String formatavalor(float d) {
        //d.getCurrencyInstance().format(12345678.90)             
        //System.out.println(d);                
        String s = (NumberFormat.getCurrencyInstance().format(d)).replace("R$ ","");
        //return  NumberFormat.getCurrencyInstance().format(d);        
        return  s;        
    }
    public String formatavalorsemmask(float d) {
        DecimalFormat formatoDois = new DecimalFormat("0.##", new DecimalFormatSymbols (new Locale ("pt", "BR")));  
        formatoDois.setMinimumFractionDigits(2);   
        formatoDois.setParseBigDecimal (true);    
        String s = (formatoDois.format(d));  
        s = s.replace("RS ","");
        return s.replace(",",".");
    }
    public String formatavalorsemmaskS(String d) {
        DecimalFormat formatoDois = new DecimalFormat("0.##", new DecimalFormatSymbols (new Locale ("pt", "BR")));  
        formatoDois.setMinimumFractionDigits(2);   
        formatoDois.setParseBigDecimal (true);    
//        String s = (formatoDois.format(d));  
        String s="",j = "";          
        s = d.replace("RS ","");
         s.replace(",",".");
        j = s;
        //System.out.println(j);
        return j;
    }
    
    public float formatavalorsemmaskS(float d) {
        DecimalFormat formatoDois = new DecimalFormat("0.##", new DecimalFormatSymbols (new Locale ("pt", "BR")));  
        formatoDois.setMinimumFractionDigits(2);   
        formatoDois.setParseBigDecimal (true);    
        String s = (formatoDois.format(d));  
        s = s.replace("RS ","");
        s = s.replace(",",".");
        return Float.parseFloat(s);       
    }

    public String getDateHoraChooser(JDateChooser obj){
        int hor = obj.getCalendar().get(Calendar.HOUR_OF_DAY);
        int mi  = obj.getCalendar().get(Calendar.MINUTE);
        String hora = "", min="" ;
        
        hora = Integer.toString(hor);
        min = Integer.toString(mi);    
        if(hor < 10)
            hora = "0"+hor;

        if(mi < 10)
            min = "0" + mi;    
        
        //Mes = Mes +1;/*Adaptaçlão, a rotina só traz ate agora 27/10/14 p mes - 1 */
        return getDateChooser(obj) +" "+ hora+":"+min;
        
    }

    public Date getDateChooserBDDT(JDateChooser obj){                
        try {
        int Dia = obj.getCalendar().get(Calendar.DAY_OF_MONTH);
        int Mes = obj.getCalendar().get(Calendar.MONTH);
        int Ano = obj.getCalendar().get(Calendar.YEAR);                
        Mes = Mes +1;/*Adaptaçlão, a rotina só traz ate agora 27/10/14 p mes - 1 */
        String _Dia = Integer.toString(Dia);
        String _Mes = Integer.toString(Mes);        
//        JOptionPane.showMessageDialog(obj, Dia);        
        if(_Dia.length() == 1) _Dia = "0"+_Dia;
        if(_Mes.length() == 1) _Mes = "0"+_Mes;                             

        
        String sValue = Ano+"-"+_Mes+"-"+ _Dia;
        java.sql.Date dtValue = java.sql.Date.valueOf(sValue);             

        return dtValue;
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(obj,"Erro converção getDateChooserBDDT"+ ex.getMessage());          
            return null;
        }
    }

    public String getDateChooserBD(JDateChooser obj){                        
        try {
        int Dia = obj.getCalendar().get(Calendar.DAY_OF_MONTH);
        int Mes = obj.getCalendar().get(Calendar.MONTH);
        int Ano = obj.getCalendar().get(Calendar.YEAR);                
        Mes = Mes +1;/*Adaptaçlão, a rotina só traz ate agora 27/10/14 p mes - 1 */
        String _Dia = Integer.toString(Dia);
        String _Mes = Integer.toString(Mes);        
//        JOptionPane.showMessageDialog(obj, Dia);        
        if(_Dia.length() == 1) _Dia = "0"+_Dia;
        if(_Mes.length() == 1) _Mes = "0"+_Mes;                             
        
        return Ano+"-"+_Mes+"-"+ _Dia;
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(obj, ex.getMessage());          
            return "";
        }
    }
    
public java.sql.Date converteDataUtilToSql(Date data) {  
  
       SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");  
  
       Date dataUtil = data;  
       java.sql.Date dataSql = null;  
  
       try {  
           dataUtil = new java.sql.Date(dataUtil.getTime());  
           dataSql = (java.sql.Date) dataUtil;  
  
       } catch (Exception e) {  
           JOptionPane.showMessageDialog(null, "Erro ao converte data para sql: " + e.getMessage());  
       }  
  
  
       return dataSql;  
   }      
   
    
}
